package cn.itsource.pethome.quartz.service.impl;

import cn.itsource.pethome.quartz.MyJob;
import cn.itsource.pethome.quartz.domain.QuartzJobInfo;
import cn.itsource.pethome.quartz.job.QuartzJob;
import cn.itsource.pethome.quartz.service.IQuartzService;
import cn.itsource.pethome.quartz.util.QuartzUtils;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.JobName;

@Service
public class QuartzServiceImpl implements IQuartzService {
    //调度器工厂
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 添加定时任务
     */
    @Override
    public void addJob(QuartzJobInfo jobInfo) {
        try {
            //获取调度器
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            //添加定时任务
            QuartzUtils.addJob(scheduler,jobInfo.getJobName(), QuartzJob.class,jobInfo,jobInfo.getCronj());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除定时任务
     */
    @Override
    public void removeJob(String jobName) {
        try {
            //获取调度器
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            //删除定时任务
            QuartzUtils.removeJob(scheduler,jobName);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
